﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMoveControl : MonoBehaviour
{
    public CameraMoveObjArg[] cameraMoveObjArgs;
    int index=0;
    float movePercent;
    // Use this for initialization
    void Start()
    {
        for(int i=0;i< cameraMoveObjArgs.Length;++i)
        {
            cameraMoveObjArgs[i].targetPosObj.GetComponent<Renderer>().enabled = false;
            cameraMoveObjArgs[i].targetLookAtObj = cameraMoveObjArgs[i].targetPosObj.transform.GetChild(0).gameObject;
            cameraMoveObjArgs[i].targetLookAtObj.GetComponent<Renderer>().enabled = false;
        }
        cameraMoveObjArgs[0].cameraStayTime = Time.time + cameraMoveObjArgs[0].cameraStayTime+1;
    }

    // Update is called once per frame
    void Update()
    {

        movePercent += Time.deltaTime * cameraMoveObjArgs[index].speed;
        movePercent = Mathf.Min(movePercent, 1);      

        Camera.main.transform.position = Vector3.Lerp(cameraMoveObjArgs[index].targetPosObj.transform.position, cameraMoveObjArgs[index+1].targetPosObj.transform.position, movePercent);
        Camera.main.transform.LookAt(Vector3.Lerp(cameraMoveObjArgs[index].targetLookAtObj.transform.position, cameraMoveObjArgs[index + 1].targetLookAtObj.transform.position, movePercent));
        if (movePercent == 1 && index< (cameraMoveObjArgs.Length-2))
        {
            if (Time.time>cameraMoveObjArgs[index].cameraStayTime)
            {
                movePercent = 0;
                index++;
                cameraMoveObjArgs[index].cameraStayTime = Time.time + cameraMoveObjArgs[index].cameraStayTime;
               
            }
        }
    }
}
[System.Serializable]
public class CameraMoveObjArg
{
    public GameObject targetPosObj;
    [System.NonSerialized]
    public GameObject targetLookAtObj;
    public float speed=0.5f;
    public float cameraStayTime = 0;
    [System.NonSerialized]
    public float cameraOriginalTime = 0;
}
